package hehe.validation.validator;

import hehe.validation.annotation.Length;
import hehe.validation.annotation.Required;
import hehe.validation.annotation.ValidatorAlias;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 字符串长度验证器
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
@ValidatorAlias(alias = "length")
public class LengthValidator extends Validator
{
    /**
     * 字符长度
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected int number = 0;

    /**
     * 操作符号
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String character;

    /**
     * 有效的操作符列表
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String[] characterList = {"gt","egt","lt","elt"};


    /**
     * 验证值接口
     *<B>说明：</B>
     *<pre>
     *　略
     *</pre>
     * @param value value
     * @return boolean
     */
    protected boolean validateValue(Object value)
    {
        String validateValue = (String)value;
        int len = this.countLength(validateValue,"utf-8");

        // 判断操作符号
        if (!Arrays.asList(this.characterList).contains(this.character)) {
            return false;
        }

        this.putMessageParams("number",this.number);
        boolean result;

        switch (this.character) {
            // 大于
            case "gt" :
                result = (len > this.number);
                break;
            // 大于等于
            case "egt" :
                result = (len >= this.number);
                break;
            // 小于
            case "lt" :
                result = (len < this.number);
                break;
            // 小于等于
            case "elt" :
                result = (len <= this.number);
                break;
            default:
                result = false;
        }

        return result;
    }

    /**
     * 计算字符长度
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected int countLength(String value,String charset)
    {
        return value.length();
    }

}
